<?php
class ModelTotalSubscriberDiscount extends Model {
    public function getTotal(&$total_data, &$total, &$taxes, $status = 0) {
        if ($this->config->get('subscriber_discount_status') && $status) {    
            $this->load->language('total/subscriber_discount');
            
            if(isset($this->session->data['guest']['email']) && $this->subscriberExists($this->session->data['guest']['email'])){					            
                $temp_total = $total_data['subtotal']['value'];
                $rate = $temp_total * -1 * ((1 * $this->config->get('subscriber_discount_fee')) / 100);
                
                $title = $this->config->get('subscriber_discount_title') ? $this->config->get('subscriber_discount_title') : $this->language->get('text_subscriber_discount');
                $total_data['subscriber_discount'] = array( 
                    'title'      => $title,
                    'text'       => $this->currency->formatPrice($rate),
                    'value'      => $rate,
                    'sort_order' => $this->config->get('subscriber_discount_sort_order')
                );
            } 
            
            $total += $rate;
            $total_data['total']['value'] += $rate;
            $total_data['total']['text'] = $this->currency->formatPrice($total_data['total']['value']);
            
            $total_data['temp_total'] = $temp_total + $rate;
        }
    }
    
    public function subscriberExists($email){
        $row = $this->db->query("SELECT * FROM " . DB_PREFIX . "newsletter WHERE email = '" . $this->db->escape($email) . "'");        
        
        return $row->num_rows ? true : false;
    }
}
?>